﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.DMS;
using System.IO;

namespace dms
{
    public partial class FolderPage : System.Web.UI.Page
    {
        public Folder FolderInfo
        {
            get
            {
                try
                {
                    if (Page.RouteData.Values["FolderID"] != null)
                        return Folder.Get(int.Parse(Page.RouteData.Values["FolderID"].ToString()));
                    else if (Request.QueryString["FolderID"] != null)
                        return Folder.Get(int.Parse(Request.QueryString["FolderID"]));
                }
                catch (Exception ex)
                {
                    //TODO: handle this
                    // usually hit when a request for a physical page cannot be found 
                    // e.g. http://www.thissite.com/NonExistantPage.aspx
                    throw ex;
                }
                
                return Folder.Root;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.Title = "Folder: " + FolderInfo.Title;
                if (Request.QueryString["View"] != null)
                {
                    switch (Request.QueryString["View"].ToLower())
                    {
                        case "thumbs":
                            ui_lstFiles.ViewMode = ViewMode.Thumbs;
                            break;
                        case "gallery":
                            ui_lstFiles.ViewMode = ViewMode.Gallery;
                            break;
                        default:
                            ui_lstFiles.ViewMode = ViewMode.List;
                            break;
                    }
                }
                else
                    ui_lstFiles.ViewMode = FolderInfo.DefaultViewMode;
                DataBind();
            }

            if (FolderInfo.ID == -1)
            {
                ui_pnlFolderView.Visible = false;
                ui_pnlRootView.Visible = true;
            }
            else
            {
                ui_pnlFolderView.Visible = true;
                ui_pnlRootView.Visible = false;
            }
        }

        public override void DataBind()
        {
            FileSystemObject[] children = FolderInfo.GetChildren();
            List<Document> childDocs = (from child in children where child is Document select child as Document).ToList();

            ui_lstFiles.DataSource = childDocs;
            ui_lstFiles.DataBind();
        }

        protected void CreateFolder(object sender, EventArgs e)
        {
            Folder newFolder = new Folder(FolderInfo.ID, ui_txtTitle.Text);
            newFolder.DefaultViewMode = (ViewMode)Enum.Parse(typeof(ViewMode), ui_ddlView.SelectedItem.Text);
            newFolder.Save();
            Response.Redirect(Request.RawUrl);
        }

        protected void ui_btnDone_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
    }
}